<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="./">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Code recipes &mdash; ib_insync 0.9.86 documentation</title>
      <link rel="stylesheet" type="text/css" href="_static/pygments.css?v=b76e3c8a" />
      <link rel="stylesheet" type="text/css" href="_static/css/theme.css?v=19f00094" />

  
    <link rel="canonical" href="https://ib_insync.readthedocs.iorecipes.html"/>
  <!--[if lt IE 9]>
    <script src="_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="_static/jquery.js?v=5d32c60e"></script>
        <script src="_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script src="_static/documentation_options.js?v=b76974d2"></script>
        <script src="_static/doctools.js?v=888ff710"></script>
        <script src="_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="_static/js/theme.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Source code" href="code.html" />
    <link rel="prev" title="Notebooks" href="notebooks.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="index.html" class="icon icon-home">
            ib_insync
          </a>
              <div class="version">
                0.9
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="readme.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">API docs</a></li>
<li class="toctree-l1"><a class="reference internal" href="notebooks.html">Notebooks</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Code recipes</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#fetching-consecutive-historical-data">Fetching consecutive historical data</a></li>
<li class="toctree-l2"><a class="reference internal" href="#scanner-data-blocking">Scanner data (blocking)</a></li>
<li class="toctree-l2"><a class="reference internal" href="#scanner-data-streaming">Scanner data (streaming)</a></li>
<li class="toctree-l2"><a class="reference internal" href="#option-calculations">Option calculations</a></li>
<li class="toctree-l2"><a class="reference internal" href="#order-book">Order book</a></li>
<li class="toctree-l2"><a class="reference internal" href="#minimum-price-increments">Minimum price increments</a></li>
<li class="toctree-l2"><a class="reference internal" href="#news-articles">News articles</a></li>
<li class="toctree-l2"><a class="reference internal" href="#news-bulletins">News bulletins</a></li>
<li class="toctree-l2"><a class="reference internal" href="#wsh-event-calendar">WSH Event Calendar</a></li>
<li class="toctree-l2"><a class="reference internal" href="#dividends">Dividends</a></li>
<li class="toctree-l2"><a class="reference internal" href="#fundemental-ratios">Fundemental ratios</a></li>
<li class="toctree-l2"><a class="reference internal" href="#short-lived-connections">Short-lived connections</a></li>
<li class="toctree-l2"><a class="reference internal" href="#integration-with-pyqt5-or-pyside2">Integration with PyQt5 or PySide2</a></li>
<li class="toctree-l2"><a class="reference internal" href="#integration-with-tkinter">Integration with Tkinter</a></li>
<li class="toctree-l2"><a class="reference internal" href="#integration-with-pygame">Integration with PyGame</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="code.html">Source code</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li>
<li class="toctree-l1"><a class="reference internal" href="links.html">Links</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">ib_insync</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="index.html" class="icon icon-home" aria-label="Home"></a></li>
      <li class="breadcrumb-item active">Code recipes</li>
      <li class="wy-breadcrumbs-aside">
            <a href="_sources/recipes.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="code-recipes">
<span id="recipes"></span><h1>Code recipes<a class="headerlink" href="#code-recipes" title="Link to this heading"></a></h1>
<p>Collection of useful patterns, snippets and recipes.</p>
<p>When using the recipes in a notebook, don’t forget to use <code class="docutils literal notranslate"><span class="pre">util.startLoop()</span></code>.</p>
<section id="fetching-consecutive-historical-data">
<h2>Fetching consecutive historical data<a class="headerlink" href="#fetching-consecutive-historical-data" title="Link to this heading"></a></h2>
<p>Suppose we want to get the 1 min bar data of Tesla since the very beginning
up until now. The best way is to start with now and keep requesting further
and further back in time until there is no more data returned.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">from</span> <span class="nn">ib_insync</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">ib</span> <span class="o">=</span> <span class="n">IB</span><span class="p">()</span>
<span class="n">ib</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">7497</span><span class="p">,</span> <span class="n">clientId</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

<span class="n">contract</span> <span class="o">=</span> <span class="n">Stock</span><span class="p">(</span><span class="s1">&#39;TSLA&#39;</span><span class="p">,</span> <span class="s1">&#39;SMART&#39;</span><span class="p">,</span> <span class="s1">&#39;USD&#39;</span><span class="p">)</span>

<span class="n">dt</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<span class="n">barsList</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
    <span class="n">bars</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqHistoricalData</span><span class="p">(</span>
        <span class="n">contract</span><span class="p">,</span>
        <span class="n">endDateTime</span><span class="o">=</span><span class="n">dt</span><span class="p">,</span>
        <span class="n">durationStr</span><span class="o">=</span><span class="s1">&#39;10 D&#39;</span><span class="p">,</span>
        <span class="n">barSizeSetting</span><span class="o">=</span><span class="s1">&#39;1 min&#39;</span><span class="p">,</span>
        <span class="n">whatToShow</span><span class="o">=</span><span class="s1">&#39;MIDPOINT&#39;</span><span class="p">,</span>
        <span class="n">useRTH</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
        <span class="n">formatDate</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">bars</span><span class="p">:</span>
        <span class="k">break</span>
    <span class="n">barsList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bars</span><span class="p">)</span>
    <span class="n">dt</span> <span class="o">=</span> <span class="n">bars</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">date</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span>

<span class="c1"># save to CSV file</span>
<span class="n">allBars</span> <span class="o">=</span> <span class="p">[</span><span class="n">b</span> <span class="k">for</span> <span class="n">bars</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">barsList</span><span class="p">)</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">bars</span><span class="p">]</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">df</span><span class="p">(</span><span class="n">allBars</span><span class="p">)</span>
<span class="n">df</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">contract</span><span class="o">.</span><span class="n">symbol</span> <span class="o">+</span> <span class="s1">&#39;.csv&#39;</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="scanner-data-blocking">
<h2>Scanner data (blocking)<a class="headerlink" href="#scanner-data-blocking" title="Link to this heading"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">allParams</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqScannerParameters</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">allParams</span><span class="p">)</span>

<span class="n">sub</span> <span class="o">=</span> <span class="n">ScannerSubscription</span><span class="p">(</span>
    <span class="n">instrument</span><span class="o">=</span><span class="s1">&#39;FUT.US&#39;</span><span class="p">,</span>
    <span class="n">locationCode</span><span class="o">=</span><span class="s1">&#39;FUT.GLOBEX&#39;</span><span class="p">,</span>
    <span class="n">scanCode</span><span class="o">=</span><span class="s1">&#39;TOP_PERC_GAIN&#39;</span><span class="p">)</span>
<span class="n">scanData</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqScannerData</span><span class="p">(</span><span class="n">sub</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">scanData</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="scanner-data-streaming">
<h2>Scanner data (streaming)<a class="headerlink" href="#scanner-data-streaming" title="Link to this heading"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">onScanData</span><span class="p">(</span><span class="n">scanData</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">scanData</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
    <span class="nb">print</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">scanData</span><span class="p">))</span>

<span class="n">sub</span> <span class="o">=</span> <span class="n">ScannerSubscription</span><span class="p">(</span>
    <span class="n">instrument</span><span class="o">=</span><span class="s1">&#39;FUT.US&#39;</span><span class="p">,</span>
    <span class="n">locationCode</span><span class="o">=</span><span class="s1">&#39;FUT.GLOBEX&#39;</span><span class="p">,</span>
    <span class="n">scanCode</span><span class="o">=</span><span class="s1">&#39;TOP_PERC_GAIN&#39;</span><span class="p">)</span>
<span class="n">scanData</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqScannerSubscription</span><span class="p">(</span><span class="n">sub</span><span class="p">)</span>
<span class="n">scanData</span><span class="o">.</span><span class="n">updateEvent</span> <span class="o">+=</span> <span class="n">onScanData</span>
<span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">60</span><span class="p">)</span>
<span class="n">ib</span><span class="o">.</span><span class="n">cancelScannerSubscription</span><span class="p">(</span><span class="n">scanData</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="option-calculations">
<h2>Option calculations<a class="headerlink" href="#option-calculations" title="Link to this heading"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">option</span> <span class="o">=</span> <span class="n">Option</span><span class="p">(</span><span class="s1">&#39;EOE&#39;</span><span class="p">,</span> <span class="s1">&#39;20171215&#39;</span><span class="p">,</span> <span class="mi">490</span><span class="p">,</span> <span class="s1">&#39;P&#39;</span><span class="p">,</span> <span class="s1">&#39;FTA&#39;</span><span class="p">,</span> <span class="n">multiplier</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>

<span class="n">calc</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">calculateImpliedVolatility</span><span class="p">(</span>
    <span class="n">option</span><span class="p">,</span> <span class="n">optionPrice</span><span class="o">=</span><span class="mf">6.1</span><span class="p">,</span> <span class="n">underPrice</span><span class="o">=</span><span class="mi">525</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">calc</span><span class="p">)</span>

<span class="n">calc</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">calculateOptionPrice</span><span class="p">(</span>
    <span class="n">option</span><span class="p">,</span> <span class="n">volatility</span><span class="o">=</span><span class="mf">0.14</span><span class="p">,</span> <span class="n">underPrice</span><span class="o">=</span><span class="mi">525</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">calc</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="order-book">
<h2>Order book<a class="headerlink" href="#order-book" title="Link to this heading"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">eurusd</span> <span class="o">=</span> <span class="n">Forex</span><span class="p">(</span><span class="s1">&#39;EURUSD&#39;</span><span class="p">)</span>
<span class="n">ticker</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqMktDepth</span><span class="p">(</span><span class="n">eurusd</span><span class="p">)</span>
<span class="k">while</span> <span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span>
        <span class="p">[</span><span class="n">d</span><span class="o">.</span><span class="n">price</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">ticker</span><span class="o">.</span><span class="n">domBids</span><span class="p">],</span>
        <span class="p">[</span><span class="n">d</span><span class="o">.</span><span class="n">price</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">ticker</span><span class="o">.</span><span class="n">domAsks</span><span class="p">])</span>
</pre></div>
</div>
</section>
<section id="minimum-price-increments">
<h2>Minimum price increments<a class="headerlink" href="#minimum-price-increments" title="Link to this heading"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">usdjpy</span> <span class="o">=</span> <span class="n">Forex</span><span class="p">(</span><span class="s1">&#39;USDJPY&#39;</span><span class="p">)</span>
<span class="n">cd</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqContractDetails</span><span class="p">(</span><span class="n">usdjpy</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">cd</span><span class="o">.</span><span class="n">marketRuleIds</span><span class="p">)</span>

<span class="n">rules</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">ib</span><span class="o">.</span><span class="n">reqMarketRule</span><span class="p">(</span><span class="n">ruleId</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">ruleId</span> <span class="ow">in</span> <span class="n">cd</span><span class="o">.</span><span class="n">marketRuleIds</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">rules</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="news-articles">
<h2>News articles<a class="headerlink" href="#news-articles" title="Link to this heading"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">newsProviders</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqNewsProviders</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">newsProviders</span><span class="p">)</span>
<span class="n">codes</span> <span class="o">=</span> <span class="s1">&#39;+&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">code</span> <span class="k">for</span> <span class="n">np</span> <span class="ow">in</span> <span class="n">newsProviders</span><span class="p">)</span>

<span class="n">amd</span> <span class="o">=</span> <span class="n">Stock</span><span class="p">(</span><span class="s1">&#39;AMD&#39;</span><span class="p">,</span> <span class="s1">&#39;SMART&#39;</span><span class="p">,</span> <span class="s1">&#39;USD&#39;</span><span class="p">)</span>
<span class="n">ib</span><span class="o">.</span><span class="n">qualifyContracts</span><span class="p">(</span><span class="n">amd</span><span class="p">)</span>
<span class="n">headlines</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqHistoricalNews</span><span class="p">(</span><span class="n">amd</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span> <span class="n">codes</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="n">latest</span> <span class="o">=</span> <span class="n">headlines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">latest</span><span class="p">)</span>
<span class="n">article</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqNewsArticle</span><span class="p">(</span><span class="n">latest</span><span class="o">.</span><span class="n">providerCode</span><span class="p">,</span> <span class="n">latest</span><span class="o">.</span><span class="n">articleId</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">article</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="news-bulletins">
<h2>News bulletins<a class="headerlink" href="#news-bulletins" title="Link to this heading"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">ib</span><span class="o">.</span><span class="n">reqNewsBulletins</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
<span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">ib</span><span class="o">.</span><span class="n">newsBulletins</span><span class="p">())</span>
</pre></div>
</div>
</section>
<section id="wsh-event-calendar">
<h2>WSH Event Calendar<a class="headerlink" href="#wsh-event-calendar" title="Link to this heading"></a></h2>
<p>A  <a class="reference external" href="https://www.wallstreethorizon.com/interactive-brokers">Wall Street Horizon subscription</a>
is needed to get corporate event data.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">ib_insync</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">ib</span> <span class="o">=</span> <span class="n">IB</span><span class="p">()</span>
<span class="n">ib</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">7497</span><span class="p">,</span> <span class="n">clientId</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

<span class="c1"># Get the conId of an instrument (IBM in this case):</span>
<span class="n">ibm</span> <span class="o">=</span> <span class="n">Stock</span><span class="p">(</span><span class="s1">&#39;IBM&#39;</span><span class="p">,</span> <span class="s1">&#39;SMART&#39;</span><span class="p">,</span> <span class="s1">&#39;USD&#39;</span><span class="p">)</span>
<span class="n">ib</span><span class="o">.</span><span class="n">qualifyContracts</span><span class="p">(</span><span class="n">ibm</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">ibm</span><span class="o">.</span><span class="n">conId</span><span class="p">)</span>  <span class="c1"># is 8314</span>

<span class="c1"># Get the list of available filters and event types:</span>
<span class="n">meta</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">getWshMetaData</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">meta</span><span class="p">)</span>

<span class="c1"># For IBM (with conId=8314) query the:</span>
<span class="c1">#   - Earnings Dates (wshe_ed)</span>
<span class="c1">#   - Board of Directors meetings (wshe_bod)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">WshEventData</span><span class="p">(</span>
    <span class="nb">filter</span> <span class="o">=</span> <span class="s1">&#39;&#39;&#39;{</span>
<span class="s1">      &quot;country&quot;: &quot;All&quot;,</span>
<span class="s1">      &quot;watchlist&quot;: [&quot;8314&quot;],</span>
<span class="s1">      &quot;limit_region&quot;: 10,</span>
<span class="s1">      &quot;limit&quot;: 10,</span>
<span class="s1">      &quot;wshe_ed&quot;: &quot;true&quot;,</span>
<span class="s1">      &quot;wshe_bod&quot;: &quot;true&quot;</span>
<span class="s1">    }&#39;&#39;&#39;</span><span class="p">)</span>
<span class="n">events</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">getWshEventData</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">events</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="dividends">
<h2>Dividends<a class="headerlink" href="#dividends" title="Link to this heading"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">contract</span> <span class="o">=</span> <span class="n">Stock</span><span class="p">(</span><span class="s1">&#39;INTC&#39;</span><span class="p">,</span> <span class="s1">&#39;SMART&#39;</span><span class="p">,</span> <span class="s1">&#39;USD&#39;</span><span class="p">)</span>
<span class="n">ticker</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqMktData</span><span class="p">(</span><span class="n">contract</span><span class="p">,</span> <span class="s1">&#39;456&#39;</span><span class="p">)</span>
<span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">ticker</span><span class="o">.</span><span class="n">dividends</span><span class="p">)</span>
</pre></div>
</div>
<p>Output:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Dividends</span><span class="p">(</span><span class="n">past12Months</span><span class="o">=</span><span class="mf">1.2</span><span class="p">,</span> <span class="n">next12Months</span><span class="o">=</span><span class="mf">1.2</span><span class="p">,</span> <span class="n">nextDate</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2019</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">6</span><span class="p">),</span> <span class="n">nextAmount</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="fundemental-ratios">
<h2>Fundemental ratios<a class="headerlink" href="#fundemental-ratios" title="Link to this heading"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">contract</span> <span class="o">=</span> <span class="n">Stock</span><span class="p">(</span><span class="s1">&#39;IBM&#39;</span><span class="p">,</span> <span class="s1">&#39;SMART&#39;</span><span class="p">,</span> <span class="s1">&#39;USD&#39;</span><span class="p">)</span>
<span class="n">ticker</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqMktData</span><span class="p">(</span><span class="n">contract</span><span class="p">,</span> <span class="s1">&#39;258&#39;</span><span class="p">)</span>
<span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">ticker</span><span class="o">.</span><span class="n">fundamentalRatios</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="short-lived-connections">
<h2>Short-lived connections<a class="headerlink" href="#short-lived-connections" title="Link to this heading"></a></h2>
<p>This IB socket protocol is designed to be used for a long-lived connection,
lasting a day or so. For short connections, where for example just a few
orders are fired of, it is best to add one second of delay before closing the
connection. This gives the connection some time to flush
the data that has not been sent yet.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">ib</span> <span class="o">=</span> <span class="n">IB</span><span class="p">()</span>
<span class="n">ib</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>

<span class="o">...</span>  <span class="c1"># create and submit some orders</span>

<span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>  <span class="c1"># added delay</span>
<span class="n">ib</span><span class="o">.</span><span class="n">disconnect</span><span class="p">()</span>
</pre></div>
</div>
</section>
<section id="integration-with-pyqt5-or-pyside2">
<h2>Integration with PyQt5 or PySide2<a class="headerlink" href="#integration-with-pyqt5-or-pyside2" title="Link to this heading"></a></h2>
<img alt="_images/qt-tickertable.png" src="_images/qt-tickertable.png" />
<p><a class="reference external" href="https://github.com/erdewit/ib_insync/blob/master/examples/qt_ticker_table.py">This example</a>
of a ticker table shows how to integrate both
realtime streaming and synchronous API requests in a single-threaded
Qt application.
The API requests in this example are <code class="docutils literal notranslate"><span class="pre">connect</span></code> and
<code class="docutils literal notranslate"><span class="pre">ib.qualifyContracts()</span></code>; The latter is used
to get the conId of a contract and use that as a unique key.</p>
<p>The Qt interface will not freeze when a request is ongoing and it is even
possible to have multiple outstanding requests at the same time.</p>
<p>This example depends on PyQt5:</p>
<p><code class="docutils literal notranslate"><span class="pre">pip3</span> <span class="pre">install</span> <span class="pre">-U</span> <span class="pre">PyQt5</span></code>.</p>
<p>It’s also possible to use PySide2 instead; To do so uncomment the PySide2
import and <code class="docutils literal notranslate"><span class="pre">util.useQt</span></code> lines in the example and comment out their PyQt5
counterparts.</p>
</section>
<section id="integration-with-tkinter">
<h2>Integration with Tkinter<a class="headerlink" href="#integration-with-tkinter" title="Link to this heading"></a></h2>
<p>To integrate with the Tkinter event loop, take a look at
<a class="reference external" href="https://github.com/erdewit/ib_insync/blob/master/examples/tk.py">this example app</a>.</p>
</section>
<section id="integration-with-pygame">
<h2>Integration with PyGame<a class="headerlink" href="#integration-with-pygame" title="Link to this heading"></a></h2>
<p>By calling <code class="docutils literal notranslate"><span class="pre">ib.sleep</span></code> from within the PyGame run loop, ib_insync can periodically
run for short whiles and keep up to date:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">ib_insync</span> <span class="k">as</span> <span class="nn">ibi</span>
<span class="kn">import</span> <span class="nn">pygame</span>


<span class="k">def</span> <span class="nf">onTicker</span><span class="p">(</span><span class="n">ticker</span><span class="p">):</span>
    <span class="n">screen</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">bg_color</span><span class="p">)</span>
    <span class="n">text</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">&#39;bid: </span><span class="si">{</span><span class="n">ticker</span><span class="o">.</span><span class="n">bid</span><span class="si">}</span><span class="s1">   ask: </span><span class="si">{</span><span class="n">ticker</span><span class="o">.</span><span class="n">ask</span><span class="si">}</span><span class="s1">&#39;</span>
    <span class="n">quote</span> <span class="o">=</span> <span class="n">font</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="n">fg_color</span><span class="p">)</span>
    <span class="n">screen</span><span class="o">.</span><span class="n">blit</span><span class="p">(</span><span class="n">quote</span><span class="p">,</span> <span class="p">(</span><span class="mi">40</span><span class="p">,</span> <span class="mi">40</span><span class="p">))</span>
    <span class="n">pygame</span><span class="o">.</span><span class="n">display</span><span class="o">.</span><span class="n">flip</span><span class="p">()</span>


<span class="n">pygame</span><span class="o">.</span><span class="n">init</span><span class="p">()</span>
<span class="n">screen</span> <span class="o">=</span> <span class="n">pygame</span><span class="o">.</span><span class="n">display</span><span class="o">.</span><span class="n">set_mode</span><span class="p">((</span><span class="mi">800</span><span class="p">,</span> <span class="mi">600</span><span class="p">))</span>
<span class="n">font</span> <span class="o">=</span> <span class="n">pygame</span><span class="o">.</span><span class="n">font</span><span class="o">.</span><span class="n">SysFont</span><span class="p">(</span><span class="s1">&#39;arial&#39;</span><span class="p">,</span> <span class="mi">48</span><span class="p">)</span>
<span class="n">bg_color</span> <span class="o">=</span> <span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">255</span><span class="p">)</span>
<span class="n">fg_color</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

<span class="n">ib</span> <span class="o">=</span> <span class="n">ibi</span><span class="o">.</span><span class="n">IB</span><span class="p">()</span>
<span class="n">ib</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
<span class="n">contract</span> <span class="o">=</span> <span class="n">ibi</span><span class="o">.</span><span class="n">Forex</span><span class="p">(</span><span class="s1">&#39;EURUSD&#39;</span><span class="p">)</span>
<span class="n">ticker</span> <span class="o">=</span> <span class="n">ib</span><span class="o">.</span><span class="n">reqMktData</span><span class="p">(</span><span class="n">contract</span><span class="p">)</span>
<span class="n">ticker</span><span class="o">.</span><span class="n">updateEvent</span> <span class="o">+=</span> <span class="n">onTicker</span>

<span class="n">running</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">while</span> <span class="n">running</span><span class="p">:</span>
    <span class="c1"># This updates IB-insync:</span>
    <span class="n">ib</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.03</span><span class="p">)</span>

    <span class="c1"># This updates PyGame:</span>
    <span class="k">for</span> <span class="n">event</span> <span class="ow">in</span> <span class="n">pygame</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">get</span><span class="p">():</span>
        <span class="k">if</span> <span class="n">event</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="n">pygame</span><span class="o">.</span><span class="n">QUIT</span><span class="p">:</span>
            <span class="n">running</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="n">pygame</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
</pre></div>
</div>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="notebooks.html" class="btn btn-neutral float-left" title="Notebooks" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="code.html" class="btn btn-neutral float-right" title="Source code" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2023, Ewald de Wit.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>